package com.od.controller;

import com.od.entity.Occupation;
import com.od.service.OccupationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("occupation")
@CrossOrigin
public class OccupationController {

    /**
     * 获取企业所属的职位信息(通过企业编号来实现),会在企业展示界面和筛选会使用
     */
    @Autowired
    private OccupationService occupationService;

    @RequestMapping("getAllOccupationByEnterpriseId")
    @ResponseBody
    public List<Map<String, Object>> getAllOccupationByEnterpriseId(String occupationEnterpriseId) {//// 用企业编号进行查询该企业的职位集合
        // System.out.println(occupationEnterpriseId);
        return occupationService.getAllOccupationByEnterpriseId(occupationEnterpriseId);
    }

    /**
     * 企业增加职位(会在企业职位发布界面中使用)
     * 
     * @param
     * @return
     */
    @PostMapping("addOccupation")
    @ResponseBody
    public String addOccupation(@RequestBody Map<String, Object> occupationData) {
        Occupation occupation = new Occupation();
        occupation.setOccupationName((String) occupationData.get("occupationName"));
        occupation.setOccupationCategory(Integer.valueOf(occupationData.get("occupationCategory").toString()));
        occupation.setOccupationCity(Integer.valueOf(occupationData.get("occupationCity").toString()));
        occupation.setOccupationLocation((String) occupationData.get("occupationLocation"));
        occupation.setOccupationProfile((String) occupationData.get("occupationProfile"));
        occupation.setOccupationDegree((String) occupationData.get("occupationDegree"));
        occupation.setOccupationEnterprise(Integer.valueOf(occupationData.get("occupationExperience").toString()));
        occupation.setOccupationSalary(Double.parseDouble(occupationData.get("occupationSalary").toString()));
        occupation.setOccupationEnterprise(Integer.valueOf(occupationData.get("occupationEnterprise").toString()));
        // System.out.println("增加的职位的学历"+occupation.getOccupationDegree());
        return occupationService.addOccupation(occupation) > 0 ? "企业增加职位成功" : "企业增加职位失败";

    }

    /**
     * 企业管理职位信息(会在企业管理职位信息界面中使用)
     * 
     * @param
     * @return
     */
    @PostMapping("upOccupation")
    @ResponseBody
    public String upOccupation(@RequestBody Map<String, Object> occupationData) {
        System.out.println(occupationData);
        Occupation occupation = new Occupation();
        occupation.setOccupationId(Integer.parseInt(occupationData.get("occupationId").toString()));
        occupation.setOccupationName(occupationData.get("occupationName").toString());
        occupation.setOccupationCategory(Integer.parseInt(occupationData.get("occupationCategory").toString()));
        occupation.setOccupationCity(Integer.parseInt(occupationData.get("occupationCity").toString()));
        occupation.setOccupationLocation(occupationData.get("occupationLocation").toString());
        occupation.setOccupationProfile(occupationData.get("occupationProfile").toString());
        String degree = occupationData.get("occupationDegree").toString();
        if ("不限".equals(degree)) {
            degree = "0";
        } else if ("初中及以下".equals(degree)) {
            degree = "1";
        } else if ("高中".equals(degree)) {
            degree = "2";
        } else if ("中专".equals(degree)) {
            degree = "3";
        } else if ("大专".equals(degree)) {
            degree = "4";
        } else if ("本科".equals(degree)) {
            degree = "5";
        } else if ("硕士".equals(degree)) {
            degree = "6";
        } else if ("博士".equals(degree)) {
            degree = "7";
        }
        occupation.setOccupationDegree(degree);

        String experience = occupationData.get("occupationExperience").toString();
        if ("不限".equals(experience)) {
            experience = "0";
        } else if ("应届生".equals(experience)) {
            experience = "-1";
        } else {
            // System.out.println(experience);
            Pattern pattern = Pattern.compile("\\d+");
            Matcher matcher = pattern.matcher(experience);
            // if (matcher.find()) {
            //     System.out.println("经验年数: " + matcher.group());
            // } else {
            //     System.out.println("没有找到匹配的经验年数");
            // }
            experience = matcher.group();
            // System.out.println(experience);
        }
        occupation.setOccupationExperience(Integer.parseInt(experience));
        occupation.setOccupationSalary(Double.parseDouble(occupationData.get("occupationSalary").toString()));
        return occupationService.upOccupation(occupation) > 0 ? "企业修改职位成功" : "企业修改职位失败";
    }

    /**
     * 根据职位Id查询职位信息*
     **/
    @RequestMapping("getOccupationByOccupationId")
    @ResponseBody
    public Map<String, Object> getOccupationByOccupationId(String occupationId) {
        return occupationService.getOccupationDetail(occupationId);
    }

    /**
     * 职位查询
     * 
     * @param data
     * @return
     */
    @PostMapping("occupationSearch")
    @ResponseBody
    public List<Map<String, Object>> occupationSearch(@RequestBody Map<String, Object> data) {
        System.out.println(data);
        return occupationService.getSearchedOccupations(data);
        // return null;
    }

    @PostMapping("enterpriseSearch")
    @ResponseBody
    public List<Map<String, Object>> enterpriseSearch(@RequestBody Map<String, Object> data) {
        // System.out.println(data.get("input").toString() + data.get("id").toString());
        return occupationService.enterpriseSearch(data.get("input").toString(), data.get("id").toString());
        // return null;
    }
    

    /**
     * 根据职位Id删除职位信息*
     **/
    @PostMapping("deOccupationByOccupationId")
    @ResponseBody
    public String deOccupationByOccupationId(@RequestBody Map<String, Integer> requestData) {
        Integer occupationId = requestData.get("occupationId");
        // System.out.println(occupationId);
        return occupationService.deOccupationByOccupationId(occupationId) > 0 ? "企业删除职位成功" : "企业删除职位失败";
    }

    /**
     * 获取全部职位信息
     * 
     * @return
     */
    @RequestMapping("allOccupations")
    @ResponseBody
    public List<Map<String, Object>> allOccupations() {
        return occupationService.getAllOccupations();
    }

    @RequestMapping("occupationCount")
    @ResponseBody
    public Integer occupationCount() {
        return occupationService.getOccupationCount();
    }

    @PostMapping("occupationDetail")
    @ResponseBody
    public Map<String, Object> occupationDetail(@RequestBody Map<String, Object> formData) {
        return occupationService.getOccupationDetail(formData.get("occupationId").toString());
    }
}
